#import "OmPersonCharacterVC.h"
#import "OmRegisterVCCharacterView.h"
@interface OmPersonCharacterVC ()
@property (nonatomic, strong) OmRegisterVCCharacterView *characterView;
@property (nonatomic, strong) UIButton *submitButton;
@end
@implementation OmPersonCharacterVC
- (void)viewDidLoad {
    [super viewDidLoad];
}
#pragma mark - ConstructionScript
- (void)configSelf {
    [self setHiddenForTabBar:YES navigationBar:NO];
    self.title = ASLocalizedString(@"性格");
}
- (void)onSubmitButtonTap {
    if (self.characterView.characters.count == 0) {
        [MBProgressHUD showErrorMessage:ASLocalizedString(@"请先选择")];
        return;
    }
    [self loadRequest];
}
#pragma mark - 网络加载
- (void)loadRequest {
    [[HUDHelper sharedInstance] loading:ASLocalizedString(@"更新中...")];
    NSString *url = [[CYURLUtils sharedCYURLUtils] makeURLWithC:@"user_api" A:@"save_user_field"];
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    NSMutableString * keyArrayString = [NSMutableString string];
    for (NSNumber *ID in self.characterView.characters) {
        [keyArrayString appendString:[NSString stringWithFormat:@"%@,", ID]];
    }
    parameters[@"key_name"] = @"character";
    parameters[@"key_value"] =  keyArrayString;
    [CYNET POST:url parameters:parameters responseCache:nil success:^(id responseObject) {
        [MBProgressHUD hideHUD];
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [MBProgressHUD showSuccessMessage:ASLocalizedString(@"个性修改成功")];
            [self changeSuccess];
        });
    } failure:^(NSString *error) {
        [MBProgressHUD hideHUD];
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [MBProgressHUD showTipMessageInView:error];
        });
    } hasCache:NO];
}
- (void)changeSuccess {
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)allocateComponents {
    self.characterView = [[OmRegisterVCCharacterView alloc] init];
    [self.view addSubview:self.characterView];
    self.submitButton = [[UIButton alloc] init].fp_addTargetActionForControlEvents(self, @selector(onSubmitButtonTap), UIControlEventTouchUpInside);
    [self.submitButton setBackgroundImage:IMAGE_NAMED(@"OmButton0") forState:UIControlStateNormal];
    [self.submitButton setTitle:ASLocalizedString(@"完成") forState:UIControlStateNormal];
    [self.submitButton setTitleEdgeInsets:UIEdgeInsetsMake(0, 0, RPX(22), 0)];
    self.submitButton.titleLabel.font = RPXFont(14);
    [self.view addSubview:self.submitButton];
}
- (void)layoutComponents {
    [self.characterView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view);
        make.left.equalTo(self.view);
    }];
    [self.submitButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.characterView.mas_bottom).offset(RPX(50));
        make.centerX.equalTo(self.view);
        make.width.equalTo(@(RPX(180)));
        make.height.equalTo(@(RPX(66)));
    }];
}
@end
